package com.wyl.mine.setting

import android.text.Html
import android.text.method.ScrollingMovementMethod
import android.view.View
import androidx.core.text.HtmlCompat.FROM_HTML_OPTION_USE_CSS_COLORS
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.MINE_HELP_DETAIL
import com.wyl.libbase.base.NormalActivity
import com.wyl.libbase.extend.autoWired
import com.wyl.libbase.extend.toast
import com.wyl.mine.R
import kotlinx.android.synthetic.main.help_detail_activity.*
import kotlinx.android.synthetic.main.help_list_activity.titlebar

class HelpDetailActivity : NormalActivity() {
    private val id by lazy { autoWired("id", "") }

    override fun getLayoutId(): Int = R.layout.help_detail_activity
    override fun initView() {
        titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }

        textview.movementMethod = ScrollingMovementMethod.getInstance()
    }

    override fun loadData() {
        OkGo.post<BaseResponse<Map<String, String>>>(MINE_HELP_DETAIL)
            .params("id", id)
            .converter(object : JsonConvert<BaseResponse<Map<String, String>>>() {})
            .adapt(ObservableBody<BaseResponse<Map<String, String>>>())
            .map { it.data }
            .subscribe({
                textview.text = Html.fromHtml(it["content"], FROM_HTML_OPTION_USE_CSS_COLORS)
            }, {
                toast(onError(it))
                it.printStackTrace()
            }).toDisposables(disposables)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.bt_close -> onBackPressed()
        }
    }

}
